import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  UseGuards,
  Req,
} from '@nestjs/common';
import { GymnasiumService } from './gymnasium.service';
import { CreateGymnasiumDto } from './dto/create-gymnasium.dto';
import { UpdateGymnasiumDto } from './dto/update-gymnasium.dto';
import { ApiBearerAuth } from '@nestjs/swagger';
import { Auth } from 'src/common/decorators/auth.decorator';
import { RequirePermission } from 'src/common/decorators/permission.decorator';



@Auth()
@ApiBearerAuth()
@Controller('gymnasium')
export class GymnasiumController {
  constructor(private readonly gymnasiumService: GymnasiumService) {}

  @RequirePermission('1005002')
  @Post('create')
  create(@Req() request, @Body() createGymnasiumDto: CreateGymnasiumDto) {
    const user = request.user;
    createGymnasiumDto.createBy = user.id;
    createGymnasiumDto.updateBy = user.id;
    if (
      createGymnasiumDto.playgrounds &&
      createGymnasiumDto.playgrounds.length > 0
    ) {
      createGymnasiumDto.playgrounds.forEach((playground) => {
        playground.createBy = user.id;
        playground.updateBy = user.id;
      });
    }
    return this.gymnasiumService.create(createGymnasiumDto);
  }

  @RequirePermission('1005002')
  @Get('bindTournament')
  bindTournament(
    @Query('tournamentId') tournamentId: number,
    @Query('gymnasiumId') gymnasiumId: number,
  ) {
    return this.gymnasiumService.bindTournament(
      gymnasiumId / 1,
      tournamentId / 1,
    );
  }
  @RequirePermission('1005001')
  @Get('getByTournamentId')
  getByTournamentId(@Query('tournamentId') tournamentId: number) {
    return this.gymnasiumService.getByTournamentId(tournamentId / 1);
  }

  // @Get()
  // findAll() {
  //   return this.gymnasiumService.findAll();
  // }

  // @Get(':id')
  // findOne(@Param('id') id: string) {
  //   return this.gymnasiumService.findOne(+id);
  // }
  @RequirePermission('1005003')
  @Patch(':id')
  update(
    @Param('id') id: string,
    @Body() updateGymnasiumDto: UpdateGymnasiumDto,
  ) {
    return this.gymnasiumService.update(+id, updateGymnasiumDto);
  }

  @RequirePermission('1005004')
  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.gymnasiumService.remove(+id);
  }
}
